<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/11/1
  Time: 16:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/mb/jsp/common.jsp" %>
<%@ page language="java" import="com.wsu.search.tools.Tools" %>
<%@ page language="java" import="com.limao.Tool.CacheClass" %>
<%@ page import="com.limao.DBDataClass.SysProjectCateDataClass" %> 
<%@ page import="com.limao.CacheManager.CrowdSysCateCache" %>
<%@ page import="com.limao.CacheManager.CrowdSysProjectCache" %>
<%@ page import="com.limao.Model.PublicData" %>
<%@ page import="com.limao.DBDataClass.CrowdSysProjectClass" %>
<%@ page import="org.apache.http.util.TextUtils" %>
<%@ page import="com.alibaba.fastjson.JSON" %>

<%!
    // 获取列表数据
    private PublicData getCrowdData(String cateid) {
        return CrowdSysProjectCache.getInstance().getGoods_ListForCommon(CacheClass.getIntValue(cateid), "", 1); 
    }

    // 组装列表项目
    private String getCrowdItemHtml(PublicData crowdData) {
        String tmpStr = "";
        if (crowdData.list != null) {
            for (int i = 0; i < crowdData.list.size(); i++) {
                CrowdSysProjectClass crowdspc = (CrowdSysProjectClass) crowdData.list.get(i);

                String introimage = "";
                if (!TextUtils.isEmpty(crowdspc.projectimage)) {
                    introimage = crowdspc.projectimage.split(";")[0];
                }
                
      			int stocknum=0; 
				if((CacheClass.getIntValue(crowdspc.limitenum)-CacheClass.getIntValue(crowdspc.supportnum))>0)
				      stocknum=CacheClass.getIntValue(crowdspc.limitenum)-CacheClass.getIntValue(crowdspc.supportnum);
				      
				String vipStrjava = "<div class=\"fl\"><img src=\"mb/images/calendar.png\" alt=\"calendar\" class=\"fl\" style=\"height: 17px;margin-top:7px;\"/><span class=\"fl dib c9 f14 mgl10\">送VIP"+crowdspc.viplevel+"级："+crowdspc.days+"天</span></div>";
                     
	            if(crowdspc.viplevel.equals("0")){
	            	vipStrjava = "";
	            }

                tmpStr += "<section class=\"proSection pr w100 bcf mgt10\" onclick=\"javascript:window.location.href='" + "mb/jsp/productDetail.jsp?projectid=" + crowdspc.projectid + "'\">\n" +
                "                <a class=\"db w100 hauto of \">\n" +
                "                    <img src=\"" + getImageBasePath(introimage) + "\" class=\"db w100 hauto\">\n" +
                "                </a>\n" +
                "                <a class=\"proName db of mgt10 f16\">" + crowdspc.projectname  + "</a>" +
			    "                <a class=\"proDetail clearfix of db lh15\"><div class=\"fl f15 orange\">&yen;" + crowdspc.packageprice + "</div></a>" +
			    "                <a class=\"proDetail clearfix of db lh2\">"+vipStrjava+"<div class=\"fr f14 orange2\">已售"+ crowdspc.supportnum +"</div></a>" +
                "            </section>";
            }
        }
        return tmpStr;
    }
%>

<%
    //取得url带过来的参数
    String cateid = Tools.RmNull(request.getParameter("cateid"));

    PublicData crowdData = getCrowdData(cateid);

    // 调用接口取得众筹分类列表数据
    List<SysProjectCateDataClass> catlist = CrowdSysCateCache.getInstance().loadProjectCateJson();
    // 增加 全部
    SysProjectCateDataClass catAll = new SysProjectCateDataClass();
    catAll.cateid = "0";
    catAll.catename = "全部";
    catlist.add(0, catAll);

    String jsonString = JSON.toJSONString(catlist);
%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>项目列表</title>

    <script>
        $(function () {
            // 进度条
            $(".bar_span").each(function () {
                var ress = $(this).attr('width');
                var len = ress + "%";
                $(this).animate({
                    width: len
                });
            })
        });

    </script>

</head>
<body ontouchstart="">
<div class="searchTop pf w100 of">
    <div class="of clearfix">
        <span class="pa" style="left: 5px;" onclick="javascript:history.back();"><img src="mb/images/top_back_white.png" style="height: 40px;width: 40px;"/></span>
        <div class="fr pr db" style="width: 85%" ;>
            <input type="search" name="keyword" value="" id="searchInput" placeholder="请输入商品名称"/  class="f14">
            <input type="submit" value="" name="" class="magnifyingGlass pa" onclick="search();">
            
            <a href="javascript:void(0)" onclick="priceRange($('#priceRangeDiv'),$('.mask-black-min'))" class="searchPriceText db pa cf"><div class="dib clearfix of"><i class="fr dib f14">价格</i><span class="searchPrice fr"></span></div></a>
        </div>
        
    </div>
</div>
<section id="priceRangeDiv" class="priceRangeDiv pf bcf w100 of">
	<div class="jz mgb10">
		<h4 class="f16 c3 mgt10">价格区间（元）</h4>
		<article class="w100 of c3 f14 mgt10">
			
		</article>
	</div>
</section>
<div class="kindNav pf bcf w100 of">
    <div class="w100  pr">
       <%
            // 组装 类别
            String tmpStr = "";
            for (SysProjectCateDataClass cat : catlist) {
                if (cateid.equals(cat.cateid)) {
                    tmpStr = tmpStr + "<span class=\"kindNavCurrent\" onclick=\"\">" + cat.catename + "</span>";
                } else {
                    tmpStr = tmpStr + "<span>" + cat.catename + "</span>";
                }
            }
            out.print(tmpStr);
        %>
    </div>
</div>
<script type="text/javascript">
    var curpage = 1;
    var keyword = "";
    var priceidVal = "";
    var cateid = <%=cateid%>;
    var total = 1;

    var catelist = JSON.parse('<%=jsonString%>');
    
   
    /**
     * 搜索
     */
    function search() {
        keyword = $("#searchInput").val();
        curpage = 1;
        getData();
    }
    
      //新添加的价格范围选择方法
	   function priceRange(priceRangeDiv,mask){

	    	priceRangeDiv.slideToggle();
	    	if(mask!=""){
	    		mask.fadeToggle();
	    	}
	    	mask.click(function(){
	    		priceRangeDiv.slideUp();
	    		mask.fadeOut();
	    	})
	    }
	    
	    function priceRangeChoose(priceBlcok,klass){
	    	priceBlcok.click(function(){
	    		$(this).addClass(klass).siblings().removeClass(klass);
	    		
	    	})
	    	
	    }
	     //调用价格区间
	    function priceRangeGet(){
		     $.ajax({
	            url: "LoginServlet",
	            data: JSON.stringify({m: 228}),
	            type: 'post',
	            dataType: 'json',
	            contentType: "application/json;charset=utf-8",
	            beforeSend: function () {
	                showProgressDg()
	            },
	            success: function (data) {
	            
	                if (data.state == 1) {
	                   showPriceLable(data.list);
	                } else {
	                    // 其他错误弹出提示
	                    showToast(data.info);
	                }
	            },
	            error: function (data) {
	                showToast(data.info);
	            },
	            complete: function () {
	                closeProgressDg();
	            }
	        });
	    }
	    
	    function showPriceLable(list){
		    
		    var htmlStr="";
		     if (list == null) {
		            return;
		        }
		        htmlStr = '';
		    $.each(list, function(index, obj) {
		    
		    	htmlStr = htmlStr+'<span onclick="javascript:priceSearch('+obj.priceid+')">'+obj.about+'</span>';
		    })  
		     $("#priceRangeDiv article").html(htmlStr);
		      priceRangeChoose($("#priceRangeDiv article span"),"current");
	    
	    }
	     //调用价格区间--end
	     
	     /**
	     * 搜索--价格区间
	     */
	    function priceSearch(priceidVal) {
	    
	    	$('#priceRangeDiv').fadeOut();
	    	$('.mask-black-min').fadeOut();
	    	
	    	
	        priceidVal = priceidVal;
	        curpage = 1;
	       
	        $("#kindContent").html('<div id="resText"></div><!--resText-->');
	        getData(priceidVal);
	       
	    }
	     
	     
	     
    // 内容列表瀑布流加载
    $(function () {
        //选择类型
        $(".kindNav>div>span").click(function () {
            var kindNameOld = $(".kindNavCurrent").text();
            var kindNameNow = $(this).text();

            if (kindNameOld != kindNameNow) {
                $(".kindNav>div>span").removeClass('kindNavCurrent');
                $(this).addClass('kindNavCurrent');

                $("#searchInput").val('');
                keyword = '';

                // 调用js的代码
                for (var i = 0; i < catelist.length; i++) {
                    if (kindNameNow == catelist[i].catename) {
                        cateid = catelist[i].cateid;
                        break;
                    }
                }
                curpage = 1;
                getData();
            }

        });
        
       
     
        //调用价格区间
        	priceRangeGet();
        	curpage = 1;
        	getData();
        	
          
        //调用价格区间--end
        
        
        
        //按回车或者键盘搜索按钮进行搜索
        $("#searchInput").on('keypress',function(e) {  
                var keycode = e.keyCode;  
                var searchName = $(this).val();  
                if(keycode=='13') {  
                    e.preventDefault();    
                    //请求搜索接口  
                    search();
                      
                }  
         });  




        $(window).bind('scroll', function () {
            show()
        });


        function show() {

            if ($(window).scrollTop() + $(window).height() >= $(document).height()) {

                loadNext();
            }

        }
    });

    /**
     * 加载下一页数据
     */
    function loadNext() {
        if (curpage < total) {
            curpage++;
            $.ajax({
                url: "LoginServlet",
                data: JSON.stringify({m: 1510, cateid: cateid, keyword: keyword, curpage: curpage,priceid:priceidVal}),
                type: 'post',
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                beforeSend: function () {
                    $("#loading").show();
                },
                success: function (data) {
                    if (data.state == 1) {
                        // 刷新页面
                        total = data.total;
                        loadMorePage(data.list);
                    } else {
                        // 其他错误弹出提示
                        showToast(data.info);
                    }
                },
                error: function (data) {
                    showToast(data.info);
                },
                complete: function () {
                    $("#loading").hide();
                }
            });
        } else {
            $("#loading").hide();
        }
    }

    /**
     * 加载更多列表页面
     * @param data
     */
    function loadMorePage(data) {
        if (data == null) {
            return;
        }

        var html = '';

        for (var i = 0; i < data.length; i++) {
            html += getItemHtml(data[i])
        }

        $("#resText").before($(html));

    }

    /**
     * 加载新数据
     */
     
    function getData(priceidVal) {
        $.ajax({
            url: "LoginServlet",
            data: JSON.stringify({m: 1510, cateid: cateid, keyword: keyword, curpage: curpage,priceid:priceidVal}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg()
            },
            success: function (data) {
                if (data.state == 1) {
                    // 刷新页面
                    total = data.total;
                    refreshPage(data.list);
                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    }

    /**
     * 刷新列表页面
     * @param data
     */
    function refreshPage(data) {
        var html = '<div id="resText"></div>'
                + '<div id="loading" class="w100">'
                + '<img src="mb/images/loading.gif" alt="正在加载" class="db jz hauto" style="width: 15px;">'
                + '<div class="w100 tc f10 c9">正在加载</div>'
                + '</div>';
        $("#kindContent").html(html);

        if (data == null) {
            return;
        }

        html = '';

        for (var i = 0; i < data.length; i++) {
            html += getItemHtml(data[i])
        }

        $("#resText").before($(html));
    }


    function getItemHtml(data) {

        var projectimage = "";
        if (data.projectimage != '') {
            projectimage = data.projectimage.split(";")[0];
        }
        
        var stocknum=0; 
		if((data.limitenum*1.0-data.supportnum*1.0)>0)
		{
			stocknum=data.limitenum*1.0-data.supportnum*1.0;
		} 	
		
		
		 var vipStr = '<div class="fl"><img src="mb/images/calendar.png" alt="calendar" class="fl" style="height: 17px;margin-top:7px;"/><span class="fl dib c9 f14 mgl10">送VIP'+data.viplevel+'级：'+data.days+'天</span></div>';
                     
            if(data.viplevel==0||data.viplevel=="0"){
            	vipStr = "";
            }

        var html = '' +
                '<section class="proSection pr w100 bcf mgt10"  onclick=javascript:window.location.href="mb/jsp/productDetail.jsp?projectid=' + data.projectid + '">' +
                '<a class="db w100 hauto of">' +
	                '<img src="' + getBaseImgUrl(projectimage) + '" class="db w100 hauto">' +
                '</a>' +
                '<a class="proName db of mgt10 f16">' + data.projectname  + '</a>' +
			    '<a class="proDetail clearfix of db"><div class="fl f15 orange">&yen;' + data.packageprice + '</div></a>' +
			    '<a class="proDetail clearfix of db lh2 mgb10">'+vipStr+'<div class="fr f14 orange2">已售'+ data.supportnum +'</div></a>' +
                '</section>' +
                '';

        return html;
    }

</script>
<div class="container jz">
    <div class="w100 of clearfix">
        <div id="kindContent" class="kindContent jz">

            <%
                out.print(getCrowdItemHtml(crowdData));
            %>

            <div id="resText"></div><!--resText-->
          
        </div>
          <div id="loading" class="w100">
                <img src="mb/images/loading.gif" alt="正在加载" class="db jz hauto" style="width: 15px;">
                <div class="w100 tc f10 c9">正在加载</div>
            </div>
    </div>
</div>

</body>
</html>
